using ASP;
using HoWave.Common;
using HoWave.Web;
using HoWave.Web.Shop;
using System;
using System.Data;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class Tuan_Index : Shop, IRequiresSessionState
{
	protected UserControls_cmpy_Top Top1;
	protected Literal ShopName;
	protected Literal ShopAddress;
	protected Literal LbContactPhone;
	protected Repeater Repeater1;
	protected UserControls_CMYP_Foot Foot1;
	protected HtmlForm form1;
	public string WhereStr = "";
	public int PID = 0;
	public string ProductName = "";
	public decimal TuanPrice = 0m;
	public decimal StartPrice = 0m;
	public decimal ProductRebate = 0m;
	public decimal EconomicalPrice;
	public string ProductIntro = "";
	public string ImagePath = "";
	public int Buytimes = 0;
	public string GetEndDate = "";
	public int Stocks = 0;
	public string UserCity = "";
	public string UserAddress = "";
	public int ShopID = 0;
	public int UserID = 0;
	protected int ProductID = 0;
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected void Page_Init(object sender, EventArgs e)
	{
		try
		{
		}
		catch (Exception ex)
		{
			base.WriteTemErrMsg(ex.Message.ToString());
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		string text = base.Request.QueryString["ProductID"];
		if (!base.IsPostBack)
		{
			base.ShowCityArea();
			this.WhereAddress();
			this.PopularTuanBind();
			if (text != null && !string.IsNullOrEmpty(text) && text != "")
			{
				this.ProductID = int.Parse(text);
			}
			else
			{
				this.ProductID = this.GetTotayTuanProductID();
			}
			this.ShowTuanInfo(this.ProductID);
			this.GetUserAddress(this.ProductID);
			this.AddHits(this.ProductID);
		}
	}
	public void WhereAddress()
	{
		SiteConfigBll siteConfigBll = new SiteConfigBll();
		if (siteConfigBll.GetSiteString("SiteConfigInfo/CityArea") != "0")
		{
			if (base.Request.Cookies["Howave_City"] != null)
			{
				string text = HttpUtility.UrlDecode(base.Request.Cookies["Howave_City"].Values["_Name"]).ToString();
				if (text != "全国")
				{
					this.WhereStr = this.WhereStr + " and ProductPlace Like '%" + text + "%'";
				}
			}
		}
	}
	public void AddHits(int ProID)
	{
		try
		{
			this.conn.Execute("update HLShop_Product set Hits=Hits+1 where ProductID=" + ProID);
		}
		catch (Exception ex)
		{
			ex.WriteErrorLog(new string[0]);
		}
	}
	public void ShowTuanInfo(int ProductID)
	{
		decimal num = 0m;
		DataTable dataTable = this.conn.GetDataTable("select * from HLShop_Product where ProductID=" + ProductID);
		if (dataTable != null && dataTable.Rows.Count > 0)
		{
			this.ProductName = dataTable.Rows[0]["ProductName"].ToString();
			switch (int.Parse(dataTable.Rows[0]["TeamNum"].ToString()))
			{
			case 1:
				num = decimal.Parse(dataTable.Rows[0]["TeamPrice1"].ToString());
				break;
			case 2:
				num = decimal.Parse(dataTable.Rows[0]["TeamPrice2"].ToString());
				break;
			case 3:
				num = decimal.Parse(dataTable.Rows[0]["TeamPrice3"].ToString());
				break;
			case 4:
				num = decimal.Parse(dataTable.Rows[0]["TeamPrice4"].ToString());
				break;
			case 5:
				num = decimal.Parse(dataTable.Rows[0]["TeamPrice5"].ToString());
				break;
			}
			this.PID = ProductID;
			this.TuanPrice = num;
			this.StartPrice = decimal.Parse(dataTable.Rows[0]["StandPrice"].ToString());
			this.ImagePath = dataTable.Rows[0]["ProductSmallImageUrl"].ToString();
			this.ProductRebate = Math.Round(num / this.StartPrice * 10m, 2);
			this.EconomicalPrice = this.StartPrice - num;
			this.ProductIntro = dataTable.Rows[0]["ProductIntro"].ToString();
			this.Buytimes = int.Parse(dataTable.Rows[0]["Buytimes"].ToString());
			this.GetEndDate = dataTable.Rows[0]["EndDate"].ToString();
			this.Stocks = int.Parse(dataTable.Rows[0]["Stocks"].ToString());
			this.ShopID = int.Parse(dataTable.Rows[0]["ShopID"].ToString());
			this.UserID = int.Parse(dataTable.Rows[0]["SalerID"].ToString());
			DataTable dataTable2 = this.conn.GetDataTable("select * from HL_UserInfo where UserID=" + this.UserID);
			if (dataTable2 != null && dataTable2.Rows.Count > 0)
			{
				if (dataTable2.Rows[0]["MobilePhone"].ToString() != "" && dataTable2.Rows[0]["MobilePhone"] != null)
				{
					this.LbContactPhone.Text = "手机：" + dataTable2.Rows[0]["MobilePhone"].ToString();
				}
				if (dataTable2.Rows[0]["HomePhone"].ToString() != "" && dataTable2.Rows[0]["HomePhone"] != null)
				{
					this.LbContactPhone.Text = this.LbContactPhone.Text + "   电话：" + dataTable2.Rows[0]["HomePhone"].ToString();
				}
				dataTable2.Dispose();
			}
			dataTable.Dispose();
		}
		else
		{
			base.WriteCodeErrMsg("", "今日暂无团购", "../index.aspx");
		}
	}
	public int GetTotayTuanProductID()
	{
		int result = 0;
		DataTable dataTable = this.conn.GetDataTable("select ProductID from HLShop_Product where ProductID=(select max(ProductID) from HLShop_Product where  ProductType=3 and EnableSale=1 and Stocks>0 and DateDiff(s,getdate(),EndDate)>0) " + this.WhereStr);
		if (dataTable != null && dataTable.Rows.Count > 0)
		{
			result = int.Parse(dataTable.Rows[0]["ProductID"].ToString());
		}
		return result;
	}
	public void PopularTuanBind()
	{
		DataTable dataTable = this.conn.GetDataTable("select top 10 * from HLShop_Product  where ProductType=3 and EnableSale=1 and Stocks>0 and DateDiff(s,getdate(),EndDate)>0 and Hits>=10 order by Hits desc");
		this.Repeater1.DataSource = dataTable;
		this.Repeater1.DataBind();
		dataTable.Dispose();
	}
	public void GetUserAddress(int ProductID)
	{
		DataTable dataTable = this.conn.GetDataTable("select a.*,c.ShopName from HL_UserInfo as a,HLShop_Product as b,HLShop_Shop as c where a.UserID=b.SalerID and a.UserID=c.ShopOwnerID and b.ProductID=" + ProductID);
		if (dataTable != null && dataTable.Rows.Count > 0)
		{
			this.UserCity = dataTable.Rows[0]["City"].ToString();
			this.UserAddress = dataTable.Rows[0]["Address"].ToString();
			this.ShopName.Text = dataTable.Rows[0]["ShopName"].ToString();
			this.ShopAddress.Text = dataTable.Rows[0]["Province"].ToString() + dataTable.Rows[0]["Address"].ToString();
		}
		dataTable.Dispose();
	}
}
